package com.rwx.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

import java.io.ByteArrayInputStream;


public class AliOssUtil {
//    endpoint: https://oss-cn-beijing.aliyuncs.com
//    accessKeyId: LTAI5tHpHnvHeo3Dmr1q6s2Z
//    accessKeySecret: WpxaKN93suNSxbWfbluxDErU4ls6HN
//    bucketName: web-alias
    /**

     try {
     String originalFilename = file.getOriginalFilename();
     String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
     String objectName=UUID.randomUUID()+extention;
     String filePath = aliOssUtil.upload(file.getBytes(), objectName);
     return Result.success(filePath);
     } catch (IOException e) {
     log.error("文件上传失败");

     }
     */

    private static String endpoint="https://oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId="LTAI5tHpHnvHeo3Dmr1q6s2Z";
    private static String accessKeySecret="WpxaKN93suNSxbWfbluxDErU4ls6HN";
    private static String bucketName="web-alias";

    /**
     * 文件上传
     *
     * @param bytes
     * @param objectName
     * @return
     */
    public static String upload(byte[] bytes, String objectName) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        String url = endpoint.split("//")[0] + "//"
                + bucketName + "."
                + endpoint.split("//")[1] + "/" + objectName;
        //https://web-alias.oss-cn-beijing.aliyuncs.com/917897f9-f9dd-4bf8-a0a3-118040d3af51.png
        //文件访问路径规则 https://BucketName.Endpoint/ObjectName
//        StringBuilder stringBuilder = new StringBuilder("https://");
//        stringBuilder
//                .append(bucketName)
//                .append(".")
//                .append(endpoint)
//                .append("/")
//                .append(objectName);
//
//        log.info("文件上传到:{}", stringBuilder.toString());
//        log.info("文件上传到:{}", url);
        //https://web-alias.
        // https://oss-cn-beijing.aliyuncs.com/3aedd075-197e-40c1-9e7e-07bbb2e92201.png

        //return stringBuilder.toString();
        return url;
    }

    public static void deleteFileFromOOS(String picName) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 删除文件。
            ossClient.deleteObject(bucketName, picName);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
